<template>
	<sc-dialog v-model="visible" :title="titleMap[mode]" show-fullscreen destroy-on-close width="750px" @close="close">
		<el-form ref="formRef" label-width="100px" :model="formData" :rules="rules">
			<el-form-item label="所属产线" prop="lineId">
				<el-select v-model="formData.lineId" :disabled="mode == 'edit'" placeholder="请选择产线">
					<el-option v-for="item in lineOptions" :key="item.id" :label="item.lineCode" :value="item.id" />
				</el-select>
			</el-form-item>
			<el-form-item label="路线编号" prop="routingCode">
				<el-input
					v-model.trim="formData.routingCode"
					placeholder="请输入工艺路线编号"
					:maxlength="32"
					show-word-limit
					:disabled="mode == 'edit'"
					clearable>
				</el-input>
			</el-form-item>
			<el-form-item label="路线名称" prop="routingName">
				<el-input
					v-model.trim="formData.routingName"
					placeholder="请输入工艺路线名称"
					:maxlength="32"
					show-word-limit
					clearable>
				</el-input>
			</el-form-item>
		</el-form>

		<template #footer>
			<el-button @click="close">取 消</el-button>
			<el-button :loading="isSaveing" type="primary" @click="save">确 定</el-button>
		</template>
	</sc-dialog>
</template>

<script>
export default {
    emits: ['complete'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			isSaveing: false,
			visible: false,
			lineOptions:[],
			formData: {
				id: 0,
				lineId: undefined,
				lineCode: '',
				routingCode: '',
				routingName: '',
			},
			rules: {
				lineId: [{ required: true, message: "请选择产线", trigger: "blur" }],
				routingCode: [{ required: true, message: "请输入工艺路线编号", trigger: "blur" }],
				routingName: [{ required: true, message: "请输入工艺路线名称", trigger: "blur" }],
			},
		};
	},
	mounted() {
	},
	methods: {
		async open(row) {
			if (!row) {
				this.mode = "add";
			} else {
				this.mode = "edit";
				let resp = await this.$API.procrouting.model.get(row.id);
				this.formData = resp.data;
			}
			const resp = await this.$API.mdline.list.get();
			this.lineOptions = resp.data;

			this.visible = true;
		},
		save() {
			this.$refs.formRef.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					let resp = null;
					if (this.formData.id === 0) {
						resp = await this.$API.procrouting.add.post(this.formData);
					} else {
						resp = await this.$API.procrouting.update.put(this.formData);
					}
					this.isSaveing = false;
					if (resp.code == 200) {
						this.$emit("complete");
						this.visible = false;
						this.$message.success("保存成功");
					} else {
						this.$alert(resp.message, "提示", { type: "error" });
					}
				}
			});
		},
		close() {
			this.formData = {
				id: 0,
				lineId: undefined,
				lineCode: '',
				routingCode: '',
				routingName: '',
			};
			this.$refs.formRef.resetFields();
			this.visible = false;
		},
	},
};
</script>
